<?php
/**
 * FlickrPhotosetsRemove class file
 * @package FlickrPhotosets
 */

/**
 * Removes a user from the database when Facebook notifies that they
 * have removed the app.
 * 
 * @package FlickrPhotosets
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 7.10.31
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 * @since Class available since Release 2.0
 */
class FlickrPhotosetsRemove extends FlickrPhotosets {
	
	/**
	 * Verifies that the call has been made from Facebook and launches
	 * the appropriate action.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function __construct() {
		parent::__construct();
				
		// Verify the signature and remove
		if ($this->verify_signature()) {
			
			// Log if unsuccessful
			if (!$this->remove_user()) {
				$this->log->remove($_POST['fb_sig_user'], false);
			}
			else {
				$this->log->remove($_POST['fb_sig_user']);
			}
			
		}
		else {
			$this->log_nonfb_attempt();
		}
	}
	
	/**
	 * Logs removal attempts that do not come from Facebook.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function log_nonfb_attempt() {
		
		// Will log the following information:
		$log['ip'] = $_SERVER['REMOTE_ADDR'];
		$log['method'] = $_SERVER['REQUEST_METHOD'];
		$log['request'] = $_REQUEST;
		$this->log->remove($_POST['fb_sig_user'], false, implode(';', $log));
		
	}
	
	/**
	 * Removes the user from the database. Gets the UID from fb_sig_user.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @return boolean True if the query was successful
	 */
	private function remove_user() {
		try {
			$this->facebook->api_client->profile_setFBML('', $_POST['fb_sig_user']);
		}
		catch (FacebookRestClientException $e) {
			// Don't worry about it
		}
		return $this->db->remove_user($_POST['fb_sig_user']);
	}
	
	/**
	 * Verifies the post variables to ensure the request is coming from Facebook.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @return bool True if verified
	 */
	private function verify_signature() {
		return ($_POST['fb_sig'] == Facebook::generate_sig($this->facebook->fb_params, FACEBOOK_API_SECRET)) ? true : false;
	}
	
}

?>